import os
import sys
import json
import time
import re
from kubernetes import client, config, stream
import urllib3
import hashlib
import yaml
import requests


# 禁用 InsecureRequestWarning
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

class KubernetsAPI:
    def __init__(self, kubeconfig=None,token=None,apiServer=None):
        if os.path.isfile(os.path.expanduser("~/.kube/config")): # 如果存在默认的 kubeconfig 文件，加载本地配置
            print("本地调用")
            config.load_kube_config()
        elif kubeconfig:
            kubeconfig_dict = self.parse_kubeconfig(kubeconfig) # 解析 kubeconfig 内容并创建配置对象
            config.load_kube_config_from_dict(kubeconfig_dict) # 使用 config.load_kube_config_from_dict 创建 kubeconfig 配置对象
        elif token:
            kubeconfig = client.Configuration()
            kubeconfig.host =  apiServer  # APISERVER 地址
            kubeconfig.verify_ssl = False
            kubeconfig.api_key = {"authorization": f"Bearer {token}"}
            client.Configuration.set_default(kubeconfig)
        else :
            pass
        
        try:
            self.core_api = client.CoreV1Api()
            self.apps_api = client.AppsV1Api()
            print("api接口调用验证成功.")
        except Exception as e:
            print(f"api接口调用验证失败.: {str(e)}")
            sys.exit("API接口调用验证失败.程序退出.")
        

    def parse_kubeconfig(self,kubeconfig_content):
        try:
            kubeconfig_dict = yaml.safe_load(kubeconfig_content)
            return kubeconfig_dict
        except yaml.YAMLError as e:
            raise Exception(f"Error parsing kubeconfig content: {str(e)}")
    
  
    def update_node_exporter_pods(self):
        # 定义要获取的命名空间
        namespace = "monitor"
        # 定义 label selector
        label_selector = "app=node-exporter"
        # 定义 Consul 地址
        consul_url = "http://consul.opsx.top"

        try:
            # 调用 Kubernetes API 获取 Pod 列表
            api_response = self.core_api.list_namespaced_pod(namespace, label_selector=label_selector)
            for pod in api_response.items:
                print("Pod 名称: %s \t IP: %s \t 节点: %s" % (pod.metadata.name, pod.status.pod_ip, pod.spec.node_name))
                # 定义服务注册的数据
                data = {
                    "id": f"{pod.spec.node_name}-{pod.metadata.name}",
                    "name": "node-exporter",
                    "address": f"{pod.status.pod_ip}",
                    "port": 9100,
                    "checks": [{
                        "http": f"http://{pod.status.pod_ip}:9100/metrics",
                        "interval": "5s"
                    }]
                }
                # 发送 PUT 请求以注册服务
                response = requests.put(f"{consul_url}/v1/agent/service/register", json=data)

                # 检查响应状态
                if response.status_code == 200:
                    print(f"服务 {pod.spec.node_name} 注册成功.")
                else:
                    print(f"无法注册服务 {pod.spec.node_name}. 状态码: {response.status_code}")
                    print(response.text)
        except Exception as e:
            print("获取 Pod 列表时出错: %s" % e)

    def clean_failed_instances():
        time.sleep(3)
        response = requests.get(f"{self.consul_url}/v1/health/state/critical")
        if response.status_code == 200:
            instances = response.json()
            for instance in instances:
                if instance['Status'] == 'critical':  # 如果实例状态为严重
                    service_id = instance['ServiceID']
                    requests.put(f"{self.consul_url}/v1/agent/service/deregister/{service_id}")
                    print(f"失效实例ID: {service_id}")
        else:
            print(f"无法从 Consul API 获取数据。状态码：{response.status_code}")
                


if __name__ == "__main__":
    K8s = KubernetsAPI()
    K8s.update_node_exporter_pods()
    

